<template>
  <div class="app-container">
    <!--查询表单-->
    <el-form :inline="true" class="demo-form-inline">
      <el-form-item>
        <el-input v-model="keyword" placeholder="关键词" />
      </el-form-item>

      <el-button type="primary" icon="el-icon-search" @click="getList()"
        >查询</el-button
      >

      <el-button type="default" @click="resetData()">清空</el-button>
    </el-form>

    <!-- 表格 -->
    <el-table
      v-loading="listLoading"
      :data="fans"
      element-loading-text="数据加载中"
      border
      fit
      highlight-current-row
    >
      <el-table-column label="序号" width="70" align="center">
        <template slot-scope="scope">
          {{ (currentPage - 1) * pageSize + scope.$index + 1 }}
        </template>
      </el-table-column>

      <el-table-column prop="username" label="姓名" width="120" />
      <el-table-column prop="gender" label="性别" width="50">
        <template slot-scope="scope">
          <span v-if="scope.row.gender === 0">女</span>
          <span v-else>男</span>
        </template>
      </el-table-column>
      <el-table-column prop="age" label="年龄" width="50" />
      <el-table-column prop="email" label="邮箱" width="180" />
      <el-table-column prop="education" label="学历" width="80" />
      <el-table-column prop="profession" label="职业" width="80" />
      <el-table-column property="createTime" label="注册时间">
      </el-table-column>

      <el-table-column
        label="操作"
        width="200"
        align="center"
        v-if="ROLE === 'admin'"
      >
        <template slot-scope="scope">
          <el-button type="text" @click="removeDataById(scope.row.id)"
            >删除</el-button
          >
        </template></el-table-column
      ></el-table
    >

    <!-- 分页 -->
    <el-pagination
      :current-page="currentPage"
      :page-size="pageSize"
      :total="total"
      style="padding: 30px 0; text-align: center"
      layout="total, prev, pager, next, jumper"
      @current-change="getList"
    />
  </div>
</template>
    <script>
//引入js文件
import { getPageByCondition } from "@/api/fan";
import { getInfo } from "@/api/user";
import { getToken } from "@/utils/auth";
export default {
  name: "Fan",
  data() {
    return {
      ROLE: "",
      fans: [],
      keyword: "",
      listLoading: true, //是否显示“数据加载中这几个字”
      total: 0, //数据条数
      currentPage: 1, //当前页码
      pageSize: 5, //每页显示条数
    };
  },
  created() {
    this.setUserRole();
    this.getList();
  },
  methods: {
    //设置用户角色
    setUserRole() {
      let userId = getToken();
      getInfo(userId).then((res) => {
        this.ROLE = res.data.roles[0];
      });
    },
    //分页查询
    getList(currentPage = 1) {
      //默认第一页
      this.currentPage = currentPage;
      getPageByCondition(this.currentPage, this.pageSize, this.keyword)
        .then((res) => {
          this.total = res.data.total;
          this.fans = res.data.rows;
          this.listLoading = false; //将“数据加载中”这几个字关掉
        })
        .catch((error) => {
          console.log(error);
        });
    },
    handleChange(value) {
      // console.log(value);
    },
    resetData() {
      //清空查询条件
      this.keyword = "";
      this.getList();
    },
    removeDataById(id) {
      //删除方法
      //提示信息
      this.$confirm("此操作将永久删除该用户, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          //调用删除方法
          deleteFan(id).then((res) => {
            this.$message.success("删除成功");
            this.getList();
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
  },
};
</script>
  
  <style>
.form-container {
  display: flex;
  justify-content: flex-end;
}
</style>